package com.icode.core.dto.house;

import com.icode.core.model.house.*;
import com.icode.core.model.user.City;
import com.icode.core.model.user.District;
import com.icode.core.model.user.Province;
import org.joda.time.LocalDate;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-11-6
 * Time: 下午7:06
 * To change this template use File | Settings | File Templates.
 */
public class RentDTO {
    private String guid;
    private String houseGuid;
    private String houseName;

    private String ownerName;
    private String ownerPhotoGuid;
    private String ownerPhone;

    private String imageGuid;
    private String personNum;
    private String address;

    private String information;
    private String price;
    private String days;

    private String customer;
    private String phoneNum;
    private String description;
    private String coustomerNum;
    private boolean noticeWay;

    private String roomType;
    private String titleImageGuid;
    private List<String> houseInfoImagesGuid = new ArrayList<>();
    private String myHouseGuid;

    private String checkInDate;
    private String leaveDate;
    private String houseWeek;
    private String myHouseStatus;

    private MyHouseDTO myHouseDTO;

    private List<EquipmentDTO> equipmentDTOs;
    private List<EquipmentDTO> supportingDTOs;

    public RentDTO() {
    }

    public RentDTO(String houseGuid, String houseName, String ownerName, String ownerPhotoGuid,
                   String imageGuid, String personNum, String price, String information, String days, String guid, String address, String customer, String phoneNum, String description, String roomType, List<String> houseInfoImagesGuid, String checkInDate, String leaveDate, String houseWeek, String myHouseStatus, String customerNum, String titleImageGuid, List<EquipmentDTO> equipmentDTOs, List<EquipmentDTO> supportingDTOs, MyHouseDTO myHouseDTO, String ownerPhone) {
        this.houseGuid = houseGuid;
        this.houseName = houseName;
        this.ownerName = ownerName;
        this.ownerPhotoGuid = ownerPhotoGuid;
        this.imageGuid = imageGuid;
        this.personNum = personNum;
        this.price = price;
        this.information = information;
        this.days = days;
        this.guid = guid;
        this.address = address;
        this.customer = customer;
        this.phoneNum = phoneNum;
        this.description = description;
        this.roomType = roomType;
        this.houseInfoImagesGuid = houseInfoImagesGuid;
        this.checkInDate = checkInDate;
        this.leaveDate = leaveDate;
        this.houseWeek = houseWeek;
        this.myHouseStatus = myHouseStatus;
        this.coustomerNum = customerNum;
        this.titleImageGuid = titleImageGuid;
        this.supportingDTOs = supportingDTOs;
        this.equipmentDTOs = equipmentDTOs;
        this.myHouseDTO = myHouseDTO;
        this.ownerPhone = ownerPhone;
    }

    public static List<RentDTO> toDTOs(List<Rent> rents) {
        List<RentDTO> rentDTOs = new ArrayList<>();
        for (Rent rent : rents) {
            RentDTO rentDTO = RentDTO.toDTO(rent);
            rentDTOs.add(rentDTO);
        }
        return rentDTOs;
    }

    public static RentDTO toDTO(Rent rent) {
        MyHouse myHouse = rent.myHouse();
        HouseInfo houseInfo = myHouse.houseInfo();
        String houseGuid = myHouse.guid();
        String information = rent.description();
        String ownerName = myHouse.userInfo().username();
        String ownerPhone = myHouse.userInfo().user().getUsername();
        String ownerPhotoGuid = null;
        if (myHouse.userInfo().usersInfoImage() != null) {
            ownerPhotoGuid = myHouse.userInfo().usersInfoImage().guid();
        }
        String imageGuid = myHouse.houseInfo().houseInfoImages().get(0).image().guid();
        BuildingType buildingType = houseInfo.buildingType();
        String personNum = buildingType.personNum();
        String roomType = buildingType.houseType().getLabel();
        HouseBaseInfo houseBaseInfo = houseInfo.baseInfo();


        Province province = null;
        if (houseBaseInfo.province() != null) {
            province = houseBaseInfo.province();
        }


        City city = null;
        if (houseBaseInfo.city() != null) {
            city = houseBaseInfo.city();
        }

        District district = null;
        if (houseBaseInfo.district() != null) {
            district = houseBaseInfo.district();
        }


        String area = "";
        if (houseBaseInfo.area() != null) {
            area = houseBaseInfo.area();
        }


        String address = " ";

        if (province != null) {
            address += province.name();
        }
        if (city != null) {
            address += city.name();
        }
        if (district != null) {
            address += district.name();
        }
        address += area;

        String price = rent.price();
        String houseName = houseBaseInfo.houseName();
        String days = rent.days();
        String guid = rent.guid();
        CustomerInfo customerInfo = rent.customerInfo();
        String customer = " ";
        String phoneNum = " ";
        String description = " ";
        String customerNum = "";
        if (customerInfo != null) {
            customer = customerInfo.customer();
            phoneNum = customerInfo.phoneNum();
            description = customerInfo.description();
            customerNum = customerInfo.personNum();

        }

        String titleImageGuid = houseInfo.baseInfo().titleImage().guid();

        List<String> houseInfoImagesGuid = new ArrayList<>();
        for (HouseInfoImage houseInfoImage : myHouse.houseInfo().houseInfoImages()) {
            houseInfoImagesGuid.add(houseInfoImage.image().guid());
        }

        String checkInDate = rent.checkInDate();
        String leaveDate = rent.leaveDate();
        String houseWeek = myHouse.houseWeek().name();
        String myHouseStatus = " ";

        MyHouseDTO myHouseDTO = MyHouseDTO.toDTO(rent.myHouse());

        if (rent.myHouseStatus() != null) {
            myHouseStatus = rent.myHouseStatus().getLabel();
        }
        List<HouseInfoEquipment> houseInfoEquipment = houseInfo.equipment();
        List<HouseInfoSupporting> houseInfoSupportings = houseInfo.supporting();
        List<EquipmentDTO> equipmentDTOs = EquipmentDTO.toDTOs(separateEquipments(houseInfoEquipment, null));
        List<EquipmentDTO> supportingDTOs = EquipmentDTO.toDTOs(separateEquipments(null, houseInfoSupportings));

        RentDTO rentDTO = new RentDTO(houseGuid, houseName, ownerName, ownerPhotoGuid, imageGuid,
                personNum, price, information, days, guid, address, customer, phoneNum, description, roomType, houseInfoImagesGuid, checkInDate, leaveDate, houseWeek, myHouseStatus, customerNum, titleImageGuid, equipmentDTOs, supportingDTOs, myHouseDTO, ownerPhone);
        return rentDTO;
    }

    private static List<Equipment> separateEquipments(List<HouseInfoEquipment> houseInfoEquipments, List<HouseInfoSupporting> houseInfoSupportings) {
        List<Equipment> equipments = new ArrayList<>();
        List<Equipment> supportings = new ArrayList<>();
        if (houseInfoSupportings == null) {
            for (HouseInfoEquipment houseInfoEquipment : houseInfoEquipments) {
                equipments.add(houseInfoEquipment.equipment());
            }
        } else {
            for (HouseInfoSupporting houseInfoEquipment : houseInfoSupportings) {
                supportings.add(houseInfoEquipment.equipment());
            }
        }
        return equipments.isEmpty() ? supportings : equipments;
    }

    public Rent toRent(MyHouse myHouse, String leaveDate, String checkInDate) {
        String information = this.information;
        String price = this.price;
        String days = this.days;
        Rent rent = new Rent(myHouse, price, days, information, leaveDate, checkInDate);
        return rent;
    }

    public Rent toRent(MyHouse myHouse, String leaveDate, String checkInDate, MyHouseStatus myHouseStatus) {
        String information = this.information;
        String price = this.price;
        String days = this.days;
        Rent rent = new Rent(myHouse, price, days, information, leaveDate, checkInDate, myHouseStatus);
        return rent;
    }

    public String getHouseGuid() {
        return houseGuid;
    }

    public void setHouseGuid(String houseGuid) {
        this.houseGuid = houseGuid;
    }

    public String getHouseName() {
        return houseName;
    }

    public void setHouseName(String houseName) {
        this.houseName = houseName;
    }

    public String getOwnerName() {
        return ownerName;
    }

    public void setOwnerName(String ownerName) {
        this.ownerName = ownerName;
    }

    public String getOwnerPhotoGuid() {
        return ownerPhotoGuid;
    }

    public void setOwnerPhotoGuid(String ownerPhotoGuid) {
        this.ownerPhotoGuid = ownerPhotoGuid;
    }

    public String getImageGuid() {
        return imageGuid;
    }

    public void setImageGuid(String imageGuid) {
        this.imageGuid = imageGuid;
    }

    public String getPersonNum() {
        return personNum;
    }

    public void setPersonNum(String personNum) {
        this.personNum = personNum;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getInformation() {
        return information;
    }

    public void setInformation(String information) {
        this.information = information;
    }

    public String getDays() {
        return days;
    }

    public void setDays(String days) {
        this.days = days;
    }

    public String getMyHouseGuid() {
        return myHouseGuid;
    }

    public void setMyHouseGuid(String myHouseGuid) {
        this.myHouseGuid = myHouseGuid;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String guid) {
        this.guid = guid;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getPhoneNum() {
        return phoneNum;
    }

    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCheckInDate() {
        return checkInDate;
    }

    public void setCheckInDate(String checkInDate) {
        this.checkInDate = checkInDate;
    }

    public String getLeaveDate() {
        return leaveDate;
    }

    public void setLeaveDate(String leaveDate) {
        this.leaveDate = leaveDate;
    }

    public String getMyHouseStatus() {
        return myHouseStatus;
    }

    public void setMyHouseStatus(String myHouseStatus) {
        this.myHouseStatus = myHouseStatus;
    }

    public String getHouseWeek() {
        return houseWeek;
    }

    public void setHouseWeek(String houseWeek) {
        this.houseWeek = houseWeek;
    }

    public List<String> getHouseInfoImagesGuid() {
        return houseInfoImagesGuid;
    }

    public void setHouseInfoImagesGuid(List<String> houseInfoImagesGuid) {
        this.houseInfoImagesGuid = houseInfoImagesGuid;
    }

    public String getRoomType() {
        return roomType;
    }

    public void setRoomType(String roomType) {
        this.roomType = roomType;
    }

    public String getCoustomerNum() {
        return coustomerNum;
    }

    public void setCoustomerNum(String coustomerNum) {
        this.coustomerNum = coustomerNum;
    }

    public boolean isNoticeWay() {
        return noticeWay;
    }

    public void setNoticeWay(boolean noticeWay) {
        this.noticeWay = noticeWay;
    }

    public String getTitleImageGuid() {
        return titleImageGuid;
    }

    public void setTitleImageGuid(String titleImageGuid) {
        this.titleImageGuid = titleImageGuid;
    }

    public List<EquipmentDTO> getEquipmentDTOs() {
        return equipmentDTOs;
    }

    public void setEquipmentDTOs(List<EquipmentDTO> equipmentDTOs) {
        this.equipmentDTOs = equipmentDTOs;
    }

    public List<EquipmentDTO> getSupportingDTOs() {
        return supportingDTOs;
    }

    public void setSupportingDTOs(List<EquipmentDTO> supportingDTOs) {
        this.supportingDTOs = supportingDTOs;
    }

    public MyHouseDTO getMyHouseDTO() {
        return myHouseDTO;
    }

    public void setMyHouseDTO(MyHouseDTO myHouseDTO) {
        this.myHouseDTO = myHouseDTO;
    }

    public String getOwnerPhone() {
        return ownerPhone;
    }

    public void setOwnerPhone(String ownerPhone) {
        this.ownerPhone = ownerPhone;
    }
}
